import Common from '../Common/common'
export default class GameOver extends Laya.Scene {
    [x: string]: any;
    public num

    constructor() {
        super()
        
        Laya.stage.alignV = "middle";
        //水平居中对齐，另一种写法：Laya.stage.alignH = Stage.ALIGN_CENTER;
        Laya.stage.alignH = "center";

        Laya.Event.TRIGGER_ENTER
    }

    /**
     * 监听打开场景
     */
    onOpened(args){
        Common.getRandomAd(0.65);
        // Laya.stage.event("showBannerAd");
        this.initData(args.level)
        this.changeFraction(args.fractionNum)
        this.btnEvent()
        this.updateMyInfo()
        var filter: Laya.GlowFilter = new Laya.GlowFilter("#936bed", 6, 0, 0);
        this.fraction.filters = [filter,filter]
        
        let gemstone = args.gemstoneNum
        this.gemstone.text = gemstone
        this.uploadScore()
    }

    initData( level:number ){
        this.num = null
        if (Common.getCacheItem("musicSetting") == "0") {
            this.music.skin = "images/index/closeMusic.png"
        } else {
            this.music.skin = "images/index/openMusic.png"
        }
        let text = Common.transferLevel(level)
        this.secretary.text = text
        this.getChildByName("star"+level).visible = true
    }

    /**
     * 游戏分数
     */
    changeFraction(num){
        let week = Common.getCacheItem('week')
        if (Common.getCacheItem("character") == "1" || Common.getCacheItem("character") == null){
            this.fraction.text = num
            this.num = num
        }else if(Number(Common.getCacheItem("character")) < 5){
            this.fraction.text = num + num*2/100
            this.num = num + num*2/100
        }else if(Common.getCacheItem("character") == "5" || Common.getCacheItem("character") == "6"){
            this.fraction.text = num + num*3/100
            this.num = num + num*3/100
        }
        Common.setCacheItem('fraction', this.num) 
        if(week == null || this.num >= week){
            this.week.visible = true
            Common.setCacheItem('week',this.num)
        }
    }

    /**
     * 用户信息
     */
    public updateMyInfo(){
        if(Common.getCacheItem("avatarUrl") != ''){
            this.wxHeadImg.skin = Common.getCacheItem("avatarUrl");
        }
        this.wxName.text = Common.getCacheItem("nick");
    }

    /**
     * 控制音乐
     */
    controlMusic() {
        Laya.stage.event("playSound", ["sound/tap.mp3"])
        if (Common.getCacheItem("musicSetting") == "1" || Common.getCacheItem("musicSetting") == null) {
            this.music.skin = "images/index/closeMusic.png"
            Common.setCacheItem("musicSetting", "0")
            console.log("关闭音乐")
            Laya.stage.event("stopMusic")
        } else {
            this.music.skin = "images/index/openMusic.png"
            Common.setCacheItem("musicSetting", "1")
            console.log("打开音乐")
            Laya.stage.event("playMusic", ["sound/homeBg.mp3", true])
        }
    }

    /**
     * 按钮监听
     */
    btnEvent(){
        this.return.on(Laya.Event.CLICK,this,this.returnGame);
        this.gameProp.on(Laya.Event.CLICK,this,this.openProp)
        this.rankList.on(Laya.Event.CLICK,this,this.openRankList)
        this.music.on(Laya.Event.CLICK,this,this.controlMusic)
        this.indexBtn.on(Laya.Event.CLICK,this,()=>{Laya.Scene.open("gameHome.scene",true)});
        this.share.on(Laya.Event.CLICK,this,()=>{
            if(Common.platform == "weixin"){
                Laya.stage.event("playSound",["sound/tap.mp3"])
                Common.shareTitle = "我在我要穿越中得了"+this.num+"分，不服来战"
                Laya.stage.event("wxShare",[7,''])
            }else{
                Laya.stage.event("TtShare","我在我要穿越中得了"+this.num+"分，不服来战")
            }
        })
    }

    returnGame(){
        console.log(new Date(),"1233")
        // Laya.stage.event("hideBannerAd");
        Laya.stage.event("playSound",["sound/tap.mp3"])
        Laya.Scene.open("gameView.scene");
    }

    openProp(){
        // Laya.stage.event("hideBannerAd");
        Laya.stage.event("playSound",["sound/tap.mp3"])
        Laya.Scene.open("gameProp.scene",false)
    }

    openRankList(){
        // Laya.stage.event("hideBannerAd");
        Laya.stage.event("playSound",["sound/tap.mp3"])
        Laya.Scene.open("rankingList.scene",false,{type:"over",fractionNum:this.num})
    }

    /**上传分数 */
    uploadScore() {
        var yhr = new Laya.HttpRequest();
        yhr.http.timeout = 10000;
        yhr.once(Laya.Event.ERROR, this, this.yErrorHandler);
        yhr.once(Laya.Event.COMPLETE, this, this.yCompleteHandler);
        yhr.send(Common.requestBaseUrl + "/game/uploadScore?&score="+this.num+"&token=" + Common.getCacheItem('loginToken'), "", "get", "json")
    }

    /**请求错误时调用 */
    yErrorHandler(data) {
        // Laya.stage.event("ShowMsg", "加载排行榜有问题，请重新打开!");
        console.log("请求错误:" + data);
    }

    /**请求结束时调用，获取服务器数据 */
    yCompleteHandler(data) {
        console.log("上传分数成功")
    }


}